package br.edu.univas.es2012.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import br.edu.univas.es2012.listeners.ButtonsCrud;
import br.edu.univas.es2012.listeners.ButtonsListarListener;
import br.edu.univas.es2012.listeners.ButtonsPesquisa;
import br.edu.univas.es2012.listeners.ConsultaPacientes;
import br.edu.univas.es2012.model.PacientesTO;

public class FrameConsultaPacientes extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private TitledBorder titledBorder;
	private PanelDadosConsultaPacientes panelDadosConsultaPacientes;
	private PanelListaButtons panelListaButtons;
	private PanelButtonsCrud panelButtonsCrud;
	private PanelButtonsPesquisaPacientes panelButtonsPesquisaPacientes;
	
	private GridBagConstraints panelDadosConsultaPacientesConstraints;
	private GridBagConstraints panelListaButtonsConstraints;
	private GridBagConstraints panelButtonsCrudConstraints;
	private GridBagConstraints panelButtonsPesquisaPacientesConstraints;
	
	private ArrayList<ConsultaPacientes> listeners = new ArrayList<ConsultaPacientes>();
	
	public FrameConsultaPacientes() {
		super("Consulta Pacientes");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initialize();
		pack();
		setResizable(false);
		setLocationRelativeTo(null);
	}

	private void initialize() {
		setLayout(new GridBagLayout());
		
		add(getPanelButtonsPesquisaPacientes(),getPanelButtonsPesquisaPacientesConstraints());
		add(getPanelButtonsCrud(),getPanelButtonsCrudConstraints());
		add(getPanelDadosListaPacientes(),getPanelDadosConsultaPacientesConstraints());
		add(getPanelListaButtons(),getPanelListaButtonsConstraints());
		
	}
	
	private TitledBorder getTitledBorderPesquisa(){
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		titledBorder = BorderFactory.createTitledBorder(loweredetched,"Pesquisar");
		titledBorder.setTitleJustification(TitledBorder.LEFT);
		
		return titledBorder;
	}
	
	private TitledBorder getTitledBorderCadastro(){
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		titledBorder = BorderFactory.createTitledBorder(loweredetched,"Cadastro");
		titledBorder.setTitleJustification(TitledBorder.LEFT);
		
		return titledBorder;
	}
	
	private TitledBorder getTitledBorderTabela(){
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		titledBorder = BorderFactory.createTitledBorder(loweredetched,"Dados Paciente");
		titledBorder.setTitleJustification(TitledBorder.LEFT);
		
		return titledBorder;
	}
	
	private GridBagConstraints createConstraintsPrototype(){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(4, 4, 4, 4);
		
		return gbc;
	}

	private PanelDadosConsultaPacientes getPanelDadosListaPacientes() {
		if(panelDadosConsultaPacientes == null){
			panelDadosConsultaPacientes = new PanelDadosConsultaPacientes();
			panelDadosConsultaPacientes.setBorder(getTitledBorderTabela());
		}
		return panelDadosConsultaPacientes;
	}

	private PanelListaButtons getPanelListaButtons() {
		if(panelListaButtons == null){
			panelListaButtons = new PanelListaButtons();
			panelListaButtons.addButtonsListarListener(new ButtonsListarListener() {
				
				@Override
				public void fechar() {
					for(ConsultaPacientes listener:listeners){
						listener.fechar();
					}
					
				}
			});
		}
		return panelListaButtons;
	}

	private PanelButtonsCrud getPanelButtonsCrud() {
		if(panelButtonsCrud == null){
			panelButtonsCrud = new PanelButtonsCrud();
			panelButtonsCrud.setBorder(getTitledBorderCadastro());
			panelButtonsCrud.addButtonsCrud(new ButtonsCrud() {
				
				
				
				@Override
				public void excluir(String codigo) {
					panelButtonsCrud.limpar();
					for(ConsultaPacientes listener:listeners){
						listener.excluir(codigo);
					}
					
				}
				
				@Override
				public void editar(String codigo) {
					panelButtonsCrud.limpar();
					for(ConsultaPacientes listener:listeners){
						listener.editar(codigo);
					}
					
				}

				@Override
				public void incluir() {
					// TODO Auto-generated method stub
					
				}
			});
		}
		return panelButtonsCrud;
	}
	
		private PanelButtonsPesquisaPacientes getPanelButtonsPesquisaPacientes() {
			if(panelButtonsPesquisaPacientes == null){
				panelButtonsPesquisaPacientes = new PanelButtonsPesquisaPacientes();
				panelButtonsPesquisaPacientes.setBorder(getTitledBorderPesquisa());
				panelButtonsPesquisaPacientes.addButtonsPesquisa(new ButtonsPesquisa() {
					
					@Override
					public void relacaoTela() {
						for(ConsultaPacientes listener:listeners){
							listener.relacaoTela();
						}
						
					}
					
					@Override
					public void pesquisar(String pesq1, Integer pesq3) {
						panelButtonsPesquisaPacientes.limpa();
						for(ConsultaPacientes listener:listeners){
							listener.pesquisar(pesq1,pesq3);
						}
						
					}
					
					@Override
					public void listarTudo() {
						for(ConsultaPacientes listener:listeners){
							listener.listarTudo();
						}
						
					}
					
					@Override
					public void fechar() {
						for(ConsultaPacientes listener:listeners){
							listener.cancelar();
						}
						
					}
				});
			}
		return panelButtonsPesquisaPacientes;
	}
			

	private GridBagConstraints getPanelDadosConsultaPacientesConstraints() {
		if(panelDadosConsultaPacientesConstraints == null){
			panelDadosConsultaPacientesConstraints = createConstraintsPrototype();
			panelDadosConsultaPacientesConstraints.gridx = 0;
			panelDadosConsultaPacientesConstraints.gridy = 2;
		}
		return panelDadosConsultaPacientesConstraints;
	}

	private GridBagConstraints getPanelListaButtonsConstraints() {
		if(panelListaButtonsConstraints == null){
			panelListaButtonsConstraints = createConstraintsPrototype();
			panelListaButtonsConstraints.gridx = 0;
			panelListaButtonsConstraints.gridy = 3;
		}
		return panelListaButtonsConstraints;
	}

	private GridBagConstraints getPanelButtonsCrudConstraints() {
		if(panelButtonsCrudConstraints == null){
			panelButtonsCrudConstraints = createConstraintsPrototype();
			panelButtonsCrudConstraints.gridx = 0;
			panelButtonsCrudConstraints.gridy = 1;
		}
		return panelButtonsCrudConstraints;
	}

	private GridBagConstraints getPanelButtonsPesquisaPacientesConstraints() {
		if(panelButtonsPesquisaPacientesConstraints == null){
			panelButtonsPesquisaPacientesConstraints = createConstraintsPrototype();
			panelButtonsPesquisaPacientesConstraints.gridx = 0;
			panelButtonsPesquisaPacientesConstraints.gridy = 0;
		}
		return panelButtonsPesquisaPacientesConstraints;
	}

	public void limpaDadosPacientes() {
		getPanelDadosListaPacientes().limparTabela();
		
	}

	public void setCadastroPacientesListener(ArrayList<PacientesTO> pacientes) {
		getPanelDadosListaPacientes().setPacienteTable(pacientes);
		
	}
	
	public void addPacientes(ButtonsCrud buttonCrud){
		getPanelButtonsCrud().addButtonsCrud(buttonCrud);
	}
	
	public void addConsultaPaciente(ConsultaPacientes listener){
		listeners.add(listener);
		getPanelDadosListaPacientes().addConsultaPacientes(listener);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
